﻿using System;

namespace Musi.Core.CSV
{
    public class Result<T>
    {
        private readonly T content;
        private readonly string message;

        public Result()
        {
            message = null;
        }

        public Result(T value)
        {
            content = value;
            message = null;
        }

        public Result(string errorMessage)
        {
            message = errorMessage;
        }

        public bool IsOk()
        {
            return message == null;
        }

        public bool IsErr()
        {
            return message != null;
        }

        public T Unwarp()
        {
            if (message != null)
                throw new Exception(message);

            return content;
        }

        public string GetErr()
        {
            if (message == null)
                return null;
            return message.ToString();
        }

        public static Result<T> Ok(T value)
        {
            return new Result<T>(value);
        }

        public static Result<T> Err(string message)
        {
            return new Result<T>(message);
        }
    }
}
